#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QPixmap>
#include<QProgressBar>
#include"httpmgr.h"
#include"Singleton.h"
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow),_playType(LOOPING),nowIndex(0),
    is_online_play(true)
{
    ui->setupUi(this);
    qDebug()<<QThread::currentThread();
    HttpMgr::GetInstance();
    this->setWindowFlag(Qt::FramelessWindowHint);
    http_get_thread=new HttpGetThread;
    playSongThread=new QThread;
    _mediaPlay_obj=new MediaPlay(this);
    _mediaPlay_obj->moveToThread(playSongThread);
    connect(this,&MainWindow::sig_playSong,_mediaPlay_obj,&MediaPlay::playSong);
    initWid();
    initList();
    initStack();
    initBottom();
    initConnect();
    _MediaPlayer=new QMediaPlayer(this);
    _MediaPlayer->setAudioOutput(new QAudioOutput(this));
    _MediaPlayer->audioOutput()->setVolume(0.5);
    //播放进度条变化
    connect(_MediaPlayer,&QMediaPlayer::positionChanged,this,&MainWindow::timeChange);
    QObject::connect(_MediaPlayer, &QMediaPlayer::mediaStatusChanged,[=](QMediaPlayer::MediaStatus status) {

        if (status == QMediaPlayer::EndOfMedia) {     //一个音乐播放完毕后
            qDebug()<<"the song play finished";
            nextSong();  //播放下一首
        }
        else if(status==QMediaPlayer::InvalidMedia){  //媒介无效时:当前歌曲为VIP，无法播放
            qDebug()<<"the song is vip, invalid play";
            nextSong();
        }
    });
    playSongThread->start();

}
void MainWindow::initWid()
{
    this->ui->big_btn->setIcon(QIcon(":/images/top/max1.png"));
    this->ui->big_btn->setIconSize(QSize(16,16));
    this->ui->big_btn->setStyleSheet(R"(
        QPushButton:hover{
            icon:url(:/images/top/max2.png);
        }
    )");

    this->ui->close_btn->setIcon(QIcon(":/images/top/close1.png"));
    this->ui->close_btn->setIconSize(QSize(16,16));
    this->ui->close_btn->setStyleSheet(R"(
        QPushButton:hover{
            icon:url(":/images/top/close2.png");
        }
    )");

    this->ui->small_btn->setIcon(QIcon(":/images/top/Minimize-2.png"));
    this->ui->small_btn->setIconSize(QSize(16,16));
    this->ui->small_btn->setStyleSheet(R"(
        QPushButton:hover{
            icon:url(":/images/top/Minimize-2 (1).png");
        }
    )");

    this->ui->normal_btn->setIcon(QIcon(":/images/top/Maximize-3.png"));
    this->ui->normal_btn->setIconSize(QSize(16,16));
    this->ui->normal_btn->setStyleSheet(R"(
        QPushButton:hover{
            icon:url(":/images/top/Maximize-3 (1).png");
        }
    )");

    this->ui->title_lb->setFixedSize(QSize(180,45));
    this->ui->title_lb->setScaledContents(true);
    QPixmap pixmap(":/images/top/title.png");
    pixmap = pixmap.scaled(180, 45, Qt::KeepAspectRatio, Qt::SmoothTransformation);

    this->ui->title_lb->setPixmap(pixmap);

    this->ui->btn3->setIcon(QIcon(":/images/top/pifu1.png"));
    this->ui->btn3->setIconSize(QSize(16,16));
    this->ui->btn3->setStyleSheet(R"(
        QPushButton:hover{
            icon:url(":/images/top/pifu2.png");
        }
    )");

    this->ui->btn2->setIcon(QIcon(":/images/top/shezhi1.png"));
    this->ui->btn2->setIconSize(QSize(16,16));
    this->ui->btn2->setStyleSheet(R"(
        QPushButton:hover{
            icon:url(":/images/top/shezhi2.png");
        }
    )");

    this->ui->btn1->setIcon(QIcon(":/images/top/sixin1.png"));
    this->ui->btn1->setIconSize(QSize(16,16));
    this->ui->btn1->setStyleSheet(R"(
        QPushButton:hover{
            icon:url(":/images/top/sixin2.png");
        }
    )");

    this->ui->more_btn->setIcon(QIcon(":/images/top/xiangxia1.png"));
    this->ui->more_btn->setIconSize(QSize(16,16));
    this->ui->more_btn->setStyleSheet(R"(
        QPushButton:hover{
            icon:url(":/images/top/xiangxia2.png");
        }
    )");

    this->ui->back_btn->setIcon(QIcon(":/images/top/le.png"));
    this->ui->back_btn->setIconSize(QSize(24,24));
    this->ui->back_btn->setStyleSheet(R"(
        QPushButton:hover{
            icon:url(":/images/top/le2.png");
        }
        QPushButton{
            background-color:rgba(0,0,0,10);
        }
    )");

    this->ui->voice_btn->setIcon(QIcon(":/images/top/m1.png"));
    this->ui->voice_btn->setIconSize(QSize(24,24));
    this->ui->voice_btn->setStyleSheet(R"(
        QPushButton:hover{
            icon:url(":/images/top/m2.png");
        }
        QPushButton{
            background-color:rgba(0,0,0,10);
        }
    )");

    this->ui->title_lb->setMaximumWidth(150);

    this->ui->search_edit->setStyleSheet(R"(
        QLineEdit:hover{
            background-color:rgba(0,0,0,26);
        }
        QLineEdit{
            border:0px;
             background-color:rgba(0,0,0,20);
            border-radius:8px;
            color:rgba(255,255,255,200);
            padding-left:14px;
            font:15px "宋体" rgba(255,255,255,200);
        }
    )");
    this->ui->search_edit->setPlaceholderText("我是奶龙");


    //上方状态栏
    this->ui->widget->setStyleSheet(R"(
        QWidget#widget{
            background-color:rgba(235,22,51,210);
        }
        QPushButton{
            border:none;
            background-color:none;
            border-radius:8px;
        }

    )");
}

void MainWindow::initList()
{
    ui->side_wid->setStyleSheet("background-color:rgba(247,248,255,100);");
    //为side_wid创建垂直布局
    QVBoxLayout* mainLy=new QVBoxLayout(ui->side_wid);
    ui->side_wid->setLayout(mainLy);
    mainLy->setContentsMargins(0,0,0,0);

    //创建滚动栏并添加进垂直布局
    scro=new QScrollArea(ui->side_wid);
    scro->setStyleSheet(R"(
        QScrollArea{
            border:none;
            background-color:rgba(247,248,255,100);
        }
    )");
    scro->setWidgetResizable(true);  //设置scro的唯一QWdiget随着scro的大小变化自动调整大小使其完全可见
    scro->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); //设置隐藏滑动条
    scro->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    ui->side_wid->layout()->addWidget(scro);

    //设置scro的唯一widget以及其垂直布局
    QWidget* wid=new QWidget(scro);
    scro->setWidget(wid);
    QVBoxLayout* vlay=new QVBoxLayout(wid);
    wid->setLayout(vlay);
    vlay->setContentsMargins(10,20,8,0);      //此水平布局中添加

    //添加list1并添加进垂直布局
    list1=new QListWidget(wid);
    list1->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    list1->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    list1->setFixedHeight(200);
    vlay->addWidget(list1);
    QListWidgetItem* it1=new QListWidgetItem(list1);
    it1->setText("为我推荐");
    it1->setIcon(QIcon(":/images/side/1.png"));
    QListWidgetItem* it2=new QListWidgetItem(list1);
    it2->setText("云音乐精选");
    it2->setIcon(QIcon(":/images/side/2.png"));
    QListWidgetItem* it3=new QListWidgetItem(list1);
    it3->setText("播客");
    it3->setIcon(QIcon(":/images/side/3.png"));
    QListWidgetItem* it4=new QListWidgetItem(list1);
    it4->setText("私人漫游");
    it4->setIcon(QIcon(":/images/side/4.png"));
    QListWidgetItem* it5=new QListWidgetItem(list1);
    it5->setText("社区");
    it5->setIcon(QIcon(":/images/side/5.png"));

    //添加分割符并添加进垂直布局
    QFrame* fra=new QFrame(wid);
    fra->setFrameShape(QFrame::HLine);
    fra->setFrameShadow(QFrame::Plain);
    fra->setFixedHeight(5);
    fra->setStyleSheet("color:rgba(0,0,0,20)");
    fra->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    vlay->addWidget(fra);

    //添加list2并添加进垂直布局
    list2=new QListWidget(wid);
    list2->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    list2->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    list2->setFixedHeight(280);
    vlay->addWidget(list2);
    QListWidgetItem* it6=new QListWidgetItem(list2);
    it6->setText("我喜欢的音乐");
    it6->setIcon(QIcon(":/images/side/6.png"));
    QListWidgetItem* it7=new QListWidgetItem(list2);
    it7->setText("最近播放");
    it7->setIcon(QIcon(":/images/side/7.png"));
    QListWidgetItem* it8=new QListWidgetItem(list2);
    it8->setText("我的播客");
    it8->setIcon(QIcon(":/images/side/8.png"));
    QListWidgetItem* it9=new QListWidgetItem(list2);
    it9->setText("我的收藏");
    it9->setIcon(QIcon(":/images/side/9.png"));
    QListWidgetItem* ity=new QListWidgetItem(list2);
    ity->setText("下载管理");
    ity->setIcon(QIcon(":/images/side/10.png"));
    QListWidgetItem* itt=new QListWidgetItem(list2);
    itt->setText("本地音乐");
    itt->setIcon(QIcon(":/images/side/11.png"));
    QListWidgetItem* itr=new QListWidgetItem(list2);
    itr->setText("我的音乐云盘");
    itr->setIcon(QIcon(":/images/side/12.png"));

    //添加分割符并添加进垂直布局
    QFrame* frm=new QFrame(wid);
    frm->setFrameShape(QFrame::HLine);
    frm->setFrameShadow(QFrame::Plain);
    frm->setFixedHeight(5);
    frm->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    frm->setStyleSheet("color:rgba(0,0,0,20)");
    vlay->addWidget(frm);

    //收藏的歌单布局
    QHBoxLayout* myL=new QHBoxLayout();
    QPushButton* btn1=new QPushButton(wid);
    btn1->setText("创建的歌单 v");
    QPushButton* btn2=new QPushButton(wid);
    btn2->setIcon(QIcon(":/images/side/jia1.png"));
    btn2->setFixedSize(QSize(22,22));
    btn2->setIconSize(QSize(16,16));

    myL->addWidget(btn1);
    myL->addStretch();
    myL->addWidget(btn2);


    myL->setContentsMargins(10,10,4,2);
    vlay->addLayout(myL);

    //添加分割符并添加进垂直布局
    QFrame* fry=new QFrame(wid);
    fry->setFrameShape(QFrame::HLine);
    fry->setFrameShadow(QFrame::Plain);
    fry->setFixedHeight(5);
    fry->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    fry->setStyleSheet("color:rgba(0,0,0,20)");
    vlay->addWidget(fry);


    vlay->addStretch();//布局中添加一个可伸缩的空间项，类似与弹簧


    //设置样式表
    wid->setStyleSheet(R"(
        QWidget{
            background-color:rgba(247,248,255,100);
        }
        QListWidget{
            border:none;
            font:15px "宋体";
            color:rgba(51,52,54,255);
        }
        QListWidget::item{
            min-height:38px;
            max-height:38px;
            border-radius:10px;
            padding-left:10px;
        }
        QListWidget::item:focus{
            outline:none;
            border:none;
        }
        QListWidget::item:hover{
            background-color:rgba(100,54,200,15);
        }
        QListWidget::item:selected {
            background-color: rgba(131,255,197,200);
            color: rgba(77,77,77,255);
        }
        QPushButton{
            border:none;
        }
    )");
    btn2->setStyleSheet(R"(
        QPushButton:hover{
            icon:url(:/images/side/jia2.png);
        }
    )");
    btn1->setStyleSheet(R"(
        QPushButton:hover{
            background-color:rgba(0,0,0,10);
            border-radius:8px;
        }
        QPushButton{
            font:14px "宋体";
            padding:7px 5px 7px 5px;
        }

    )");



    list1->setFocusPolicy(Qt::NoFocus);
    list2->setFocusPolicy(Qt::NoFocus);
}

void MainWindow::initStack()
{
    QVBoxLayout* vlay=new QVBoxLayout(ui->main_wid);
    vlay->setContentsMargins(0,0,0,0);

    //创建QStackWidget
    stack=new QStackedWidget(ui->main_wid);
    vlay->addWidget(stack);

    _recommend=new recommend(stack);
    _yunSelect=new YunSelected(stack);
    _podcast=new Podcast(stack);
    _browse=new browse_(stack);
    _community=new community_(stack);
    _myLove=new MyLove(stack);
    _lastMusic=new LastMusic(stack);
    _myPodcast=new MyPodcast(stack);
    _myCollection=new MyCollection(stack);
    _downLoadMgr=new DownloadMgr(stack);
    _localMusic=new LocalMusic(stack);
    _myMusicYun=new MyMusicYun(stack);
    _searchWid=new SearchWidget(stack);

    stack->addWidget(_recommend);
    stack->addWidget(_yunSelect);
    stack->addWidget(_podcast);
    stack->addWidget(_browse);
    stack->addWidget(_community);
    stack->addWidget(_myLove);
    stack->addWidget(_lastMusic);
    stack->addWidget(_myPodcast);
    stack->addWidget(_myCollection);
    stack->addWidget(_downLoadMgr);
    stack->addWidget(_localMusic);
    stack->addWidget(_myMusicYun);
    stack->addWidget(_searchWid);

    stack->setCurrentIndex(5);
    list2->setCurrentRow(0);

    //连接信号与槽
    connect(ui->back_btn_2,&QPushButton::clicked,this,&MainWindow::slot_upSong);
    connect(ui->forward_btn,&QPushButton::clicked,this,&MainWindow::slot_nextSong);
    connect(_searchWid,&SearchWidget::listDoublePress,this,&MainWindow::slot_search_doublePress);
    // connect(_myLove,&MyLove::listDoublePress,this,&MainWindow::slot_myLove_listDouble);
}

void MainWindow::initBottom()
{
    soundWid=new SoundWidget(this);
    connect(soundWid->slider,&QSlider::valueChanged,this,&MainWindow::soundChange);
    soundWid->hide();

    ui->down_wid->setFixedHeight(75);


    ui->pushButton->setIcon(QIcon(":/images/down/liebiao1.png"));
    ui->pushButton->setIconSize(QSize(24,24));

    ui->sound_btn->setIcon(QIcon(":/images/down/shengyi1.png"));
    ui->sound_btn->setIconSize(QSize(24,24));

    ui->pushButton_3->setIcon(QIcon(":/images/down/haoyou1.png"));
    ui->pushButton_3->setIconSize(QSize(24,24));

    ui->pushButton_4->setIcon(QIcon(":/images/down/yinxiao1.png"));
    ui->pushButton_4->setIconSize(QSize(24,24));

    ui->pushButton_5->setIcon(QIcon(":/images/down/yinzhi1.png"));
    ui->pushButton_5->setIconSize(QSize(28,28));

    ui->back_btn_2->setIcon(QIcon(":/images/down/shang1.png"));
    ui->back_btn_2->setIconSize(QSize(24,24));

    ui->forward_btn->setIcon(QIcon(":/images/down/xia1.png"));
    ui->forward_btn->setIconSize(QSize(24,24));

    ui->pushButton_10->setIcon(QIcon(":/images/down/kaishi.png"));
    ui->pushButton_10->setIconSize(QSize(36,36));

    ui->word_btn->setIcon(QIcon(":/images/down/ci1.png"));
    ui->word_btn->setIconSize(QSize(24,24));

    ui->mode_btn->setIcon(QIcon(":/images/down/danqu1.png"));
    ui->mode_btn->setIconSize(QSize(18,18));

    ui->pushButton_6->setIcon(QIcon(":/images/down/xihuan1.png"));
    ui->pushButton_6->setIconSize(QSize(18,18));

    ui->pushButton_7->setIcon(QIcon(":/images/down/pinglun1 (2).png"));
    ui->pushButton_7->setIconSize(QSize(18,18));

    ui->download_btn->setIcon(QIcon(":/images/down/xiazai1.png"));
    ui->download_btn->setIconSize(QSize(18,18));

    ui->pushButton_9->setIcon(QIcon(":/images/down/genduo1.png"));
    ui->pushButton_9->setIconSize(QSize(18,18));

    ui->label_2->setPixmap(QPixmap(":/images/down/Dis04.png"));
    ui->label_2->setScaledContents(true);
    ui->label_2->setFixedSize(QSize(45,45));

    ui->name_lb->setStyleSheet("font:16px \"宋体\"");
    ui->name_lb->setFixedWidth(160);

    ui->label_4->setFixedWidth(45);
    ui->label_4->setAlignment(Qt::AlignRight| Qt::AlignVCenter);
    ui->label_5->setFixedWidth(45);

    ui->down_wid->setStyleSheet(R"(
        QPushButton{
            border:none;
        }
        QPushButton#pushButton:hover{
            icon:url(:/images/down/liebiao2.png);
        }
        QPushButton#sound_btn:hover{
            icon:url(:/images/down/shengyin2.png);
        }
        QPushButton#pushButton_3:hover{
            icon:url(:/images/down/haoyou2.png);
        }
        QPushButton#pushButton_4:hover{
            icon:url(:/images/down/yinxiao2.png);
        }
        QPushButton#pushButton_5:hover{
            icon:url(:/images/down/yinzhi2.png);
        }
        QPushButton#word_btn:hover{
            icon:url(:/images/down/ci2.png);
        }
        QPushButton#back_btn_2:hover{
            icon:url(:/images/down/shang2.png);
        }
        QPushButton#forward_btn:hover{
            icon:url(:/images/down/xia2.png);
        }
        QPushButton#mode_btn:hover{
            icon:url(:/images/down/danqu2.png);
        }
        QPushButton#pushButton_6:hover{
            icon:url(:/images/down/xihuan2.png);
        }
        QPushButton#pushButton_7:hover{
            icon:url(:/images/down/pinglun2 (2).png);
        }
        QPushButton#download_btn:hover{
            icon:url(:/images/down/xiazai2.png);
        }
        QPushButton#pushButton_9:hover{
            icon:url(:/images/down/genduo2.png);
        }
        QWidget#down_wid{
            background-color: rgb(245, 255, 255);
        }
        QLabel#label_4 ,#label_5{
            font:14px "宋体";
            color:grey;
        }
    )");

    ui->down_wid->hide();
}

void MainWindow::initConnect()
{
    //右上方设置窗口槽函数
    connect(this->ui->close_btn,&QPushButton::clicked,this,&MainWindow::slot_close);
    connect(this->ui->big_btn,&QPushButton::clicked,this,&MainWindow::slot_big);
    connect(this->ui->small_btn,&QPushButton::clicked,this,&MainWindow::slot_small);
    connect(this->ui->normal_btn,&QPushButton::clicked,this,&MainWindow::slot_normal);

    //选中左侧导航栏切换主窗口
    connect(list1,&QListWidget::currentRowChanged,this,&MainWindow::slot_curchange);
    connect(list2,&QListWidget::currentRowChanged,this,&MainWindow::slot_curchange_2);
}

void MainWindow::nextSong()
{
    //先暂停并清空media里面的数据
    _MediaPlayer->stop();
    _MediaPlayer->setSource(QUrl());
    QList<songInfo>songList=_searchWid->getSongList();

    ui->pushButton_10->setIcon(QIcon(":/images/down/zanting.png"));
    if(_playType==LOOPING){
        nowIndex+=1;
        if(nowIndex==songList.size())nowIndex=0;
    }
    else if(_playType==SINGLE){
        //重复播放单曲
    }
    else if(_playType==RANDOM){
        nowIndex=QRandomGenerator::global()->bounded(-1, songList.size());
    }

    ui->name_lb->setText(songList[nowIndex].musicName);
    ui->label_4->setText("00:00");
    int minutes=songList[nowIndex].musicDuration/60000;
    int seconds=(songList[nowIndex].musicDuration%60000)/1000;
    QString tim=QString::asprintf("%02d:%02d",minutes,seconds);
    ui->label_5->setText(tim);
    // //在子线程播放音乐
    emit sig_playSong(_MediaPlayer,nowIndex,songList,&is_online_play,&is_start,ui->pushButton_10,ui->progressBar);
}

void MainWindow::playSong(int index)
{
    QList<songInfo>songList=_searchWid->getSongList();
    int count=songList[nowIndex].musicDuration;
    ui->progressBar->setRange(0,count);
    if(is_online_play){       //如果时获取的数据在线播放
        QString url;
        //通过网网易云API传入之前返回的歌曲的ID实现播放
        url=QString("https://music.163.com/song/media/outer/url?id=%1.mp3").arg(songList[index].musicId);

        qDebug()<<"loading procedure: "+url;
        _MediaPlayer->setSource(QUrl(url));
        _MediaPlayer->play();
        ui->pushButton_10->setIcon(QIcon(":/images/down/kaishi.png"));
        is_start=1;
    }
    else{
        QUrl url=QUrl(songList[index].musicId);
        _MediaPlayer->setSource(url);
        _MediaPlayer->play();
        ui->pushButton_10->setIcon(QIcon(":/images/down/kaishi.png"));
        is_start=1;
    }
}

void MainWindow::soundChange(int t)
{
    _MediaPlayer->audioOutput()->setVolume(t/100.0);
}

void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    QPoint pv=event->globalPosition().toPoint();
    if(this->windowState()==Qt::WindowMaximized&&event->pos().y()<=ui->widget->height()){
        this->showNormal();
        if(!soundWid->isHidden())soundWid->hide();
        event->accept();
        return;
    }
    else{
        if(is_press_move==true&&(pv-last).manhattanLength()>QApplication::startDragDistance()){
            this->move(pv.x()-last.x(),pv.y()-last.y());
            last=pv-this->pos();
            event->accept();
            return;
        }
    }
}

void MainWindow::mousePressEvent(QMouseEvent *event)
{
    if(event->button()==Qt::LeftButton&&event->pos().y()<=ui->widget->height()){
        is_press_move=true;
        last=event->globalPosition().toPoint()-this->pos();
        event->accept();
        return;
    }
    event->accept();
}

void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
    if(event->button()==Qt::LeftButton){
        is_press_move=false;
    }
}

void MainWindow::slot_close()
{
    qApp->exit();
}

void MainWindow::slot_big()
{
    this->showMaximized();
    if(!soundWid->isHidden()){
        soundWid->hide();
    }

}

void MainWindow::slot_small()
{
    this->showMinimized();
    if(!soundWid->isHidden()){
        soundWid->hide();
    }
}

void MainWindow::slot_normal()
{
    this->showNormal();
    if(!soundWid->isHidden()){
        soundWid->hide();
    }
}

void MainWindow::slot_curchange(int t)
{
    list2->setCurrentRow(-1);
    stack->setCurrentIndex(t);
}

void MainWindow::slot_curchange_2(int t)
{
    list1->setCurrentRow(-1);
    stack->setCurrentIndex(t+list1->count());
}

MainWindow::~MainWindow()
{
    http_get_thread->quit();

    http_get_thread->wait();

    delete ui;
}

void MainWindow::on_search_edit_returnPressed()
{
    if(http_get_thread->isRunning()){
        http_get_thread->quit();
        http_get_thread->wait();
        qDebug()<<1;
    }
    QString str=ui->search_edit->text();
    //接入网易云API
    str="http://music.163.com/api/search/get/web?csrf_token=hlpretag=&hlposttag=&s={"+str+"}&type=1&offset=0&total=true&limit=100";
    http_get_thread->setData(QUrl(str),ReqId::ID_SEARCH_SONG_CODE,Modules::SEARCHMUSICMODE);
    http_get_thread->start();

    //设置主界面stack切换
    list1->setCurrentRow(-1);
    list2->setCurrentRow(-1);
    stack->setCurrentIndex(12);
    _searchWid->getSongLab()->setText(ui->search_edit->text());

}

void MainWindow::timeChange(int t)
{
    int minutes=t/60000;
    int seconds=(t%60000)/1000;
    QString tim=QString::asprintf("%02d:%02d",minutes,seconds);
    ui->label_4->setText(tim);
    ui->progressBar->setValue(t);
}

void MainWindow::slot_search_doublePress(int index)
{
    ui->pushButton_10->setIcon(QIcon(":/images/down/zanting.png"));
    is_start=0;

    is_online_play=true;        //本次列表的歌曲为在线播放
    nowIndex=index;
    QList<songInfo>songList=_searchWid->getSongList();

    ui->name_lb->setText(songList[index].musicName);
    ui->label_4->setText("00:00");
    int minutes=songList[index].musicDuration/60000;
    int seconds=(songList[index].musicDuration%60000)/1000;
    QString tim=QString::asprintf("%02d:%02d",minutes,seconds);
    ui->label_5->setText(tim);

    if(ui->down_wid->isHidden()) ui->down_wid->show();
    // //在子线程播放音乐
    // if(playSongThread->isRunning()){
    //     playSongThread->quit();
    //     playSongThread->wait();
    // }
    // playSongThread->start();
    emit sig_playSong(_MediaPlayer,nowIndex,songList,&is_online_play,&is_start,ui->pushButton_10,ui->progressBar);

}


MediaPlay::MediaPlay(QObject *parent):QObject(parent)
{

}

void MediaPlay::playSong(QMediaPlayer *MediaPlayer, int index, QList<songInfo> songList,
                         bool* is_online_play,bool* is_start,QPushButton*button,QProgressBar* progressbar)
{
    qDebug()<<QThread::currentThread();
    int count=songList[index].musicDuration;
    progressbar->setRange(0,count);
    if(*is_online_play){       //如果时获取的数据在线播放
        QString url;
        //通过网网易云API传入之前返回的歌曲的ID实现播放
        url=QString("https://music.163.com/song/media/outer/url?id=%1.mp3").arg(songList[index].musicId);

        qDebug()<<"loading procedure: "+url;
        MediaPlayer->setSource(QUrl(url));
        MediaPlayer->play();
        button->setIcon(QIcon(":/images/down/kaishi.png"));
        *is_start=1;
    }
    else{
        QUrl url=QUrl(songList[index].musicId);
        MediaPlayer->setSource(url);
        MediaPlayer->play();
        button->setIcon(QIcon(":/images/down/kaishi.png"));
        *is_start=1;
    }
}

void MainWindow::on_sound_btn_clicked()
{
    QPoint P=ui->sound_btn->mapTo(this,QPoint(0, 0));
    QPoint globalP=mapToGlobal(P);
    int x=globalP.x()+ui->sound_btn->width()/2-soundWid->width()/2;
    int y=globalP.y()-soundWid->height();

    soundWid->move(x,y-15);

    //设置音量
    float s=_MediaPlayer->audioOutput()->volume();
    //volume()返回的float值是 0~1.0，当其为1.0时音量到达计算机系统当前的音量值。

    soundWid->slider->setValue(s*100);

    if(soundWid->isHidden())soundWid->show();
    else soundWid->hide();
}


void MainWindow::on_pushButton_10_clicked()
{
    if(is_start){
        is_start=0;
        _MediaPlayer->pause();
        ui->pushButton_10->setIcon(QIcon(":/images/down/zanting.png"));
    }
    else{
        is_start=1;
        _MediaPlayer->play();
        ui->pushButton_10->setIcon(QIcon(":/images/down/kaishi.png"));
    }
}


void MainWindow::on_mode_btn_clicked()
{
    if(_playType==SINGLE){
        _playType=LOOPING;
        ui->mode_btn->setIcon(QIcon(":/images/down/danqu1.png"));
        ui->mode_btn->setStyleSheet(R"(
            QPushButton:hover{
                icon:url(:/images/down/danqu2.png);
            }
        )");
    }
    else if(_playType==LOOPING){
        _playType=RANDOM;
        ui->mode_btn->setIcon(QIcon(":/images/down/suiji1.png"));
        ui->mode_btn->setStyleSheet(R"(
            QPushButton:hover{
                icon:url(:/images/down/suiji2.png);
            }
        )");
    }
    else if(_playType==RANDOM){
        _playType=SINGLE;
        ui->mode_btn->setIcon(QIcon(":/images/down/xunhuan1.png"));
        ui->mode_btn->setStyleSheet(R"(
            QPushButton:hover{
                icon:url(:/images/down/xunhuan2.png);
            }
        )");
    }
}

void MainWindow::slot_nextSong()
{
    QList<songInfo>songList=_searchWid->getSongList();
    ui->pushButton_10->setIcon(QIcon(":/images/down/zanting.png"));
    if(_playType==LOOPING){
        nowIndex+=1;
        if(nowIndex==songList.size())nowIndex=0;
    }
    else if(_playType==SINGLE){
        //重复播放单曲
    }
    else if(_playType==RANDOM){
        nowIndex=QRandomGenerator::global()->bounded(-1, songList.size());
    }

    ui->name_lb->setText(songList[nowIndex].musicName);
    ui->label_4->setText("00:00");
    int minutes=songList[nowIndex].musicDuration/60000;
    int seconds=(songList[nowIndex].musicDuration%60000)/1000;
    QString tim=QString::asprintf("%02d:%02d",minutes,seconds);
    ui->label_5->setText(tim);
    // //在子线程播放音乐
    emit sig_playSong(_MediaPlayer,nowIndex,songList,&is_online_play,&is_start,ui->pushButton_10,ui->progressBar);
}

void MainWindow::slot_upSong()
{
    QList<songInfo>songList=_searchWid->getSongList();
    ui->pushButton_10->setIcon(QIcon(":/images/down/zanting.png"));
    if(_playType==LOOPING){
        nowIndex-=1;
        if(nowIndex<0)nowIndex=songList.size()-1;
    }
    else if(_playType==SINGLE){
        //重复播放单曲
    }
    else if(_playType==RANDOM){
        nowIndex=QRandomGenerator::global()->bounded(-1, songList.size());
    }

    ui->name_lb->setText(songList[nowIndex].musicName);
    ui->label_4->setText("00:00");
    int minutes=songList[nowIndex].musicDuration/60000;
    int seconds=(songList[nowIndex].musicDuration%60000)/1000;
    QString tim=QString::asprintf("%02d:%02d",minutes,seconds);
    ui->label_5->setText(tim);
    // //在子线程播放音乐
    emit sig_playSong(_MediaPlayer,nowIndex,songList,&is_online_play,&is_start,ui->pushButton_10,ui->progressBar);
}

